//
//  HMHomeBottomView.swift
//  WB25
//
//  Created by HM on 2017/7/4.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

class HMHomeBottomView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        
        let retweetButton = addChildButton(imgName: "timeline_icon_retweet", title: "转发")
        let commentButton = addChildButton(imgName: "timeline_icon_comment", title: "评论")
        let unlikeButton = addChildButton(imgName: "timeline_icon_unlike", title: "赞")
        
        
        let line1 = addChildLine()
        let line2 = addChildLine()
        
        // 添加约束
        retweetButton.snp.makeConstraints { (make) in
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        
        commentButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(retweetButton.snp.right)
            make.width.equalTo(unlikeButton)
        }
        
        unlikeButton.snp.makeConstraints { (make) in
            make.top.right.bottom.equalTo(self)
            make.left.equalTo(commentButton.snp.right)
        }
        
        line1.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetButton.snp.right)
        }
        
        line2.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.centerX.equalTo(unlikeButton.snp.left)
        }
        
    }

}

extension HMHomeBottomView{
    //MARK: - 抽取一个创建button的公共方法
    fileprivate func addChildButton(imgName: String, title: String) -> UIButton{
        let button = UIButton()
        button.setTitle(title, for: UIControlState.normal)
        button.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        button.setTitleColor(HMThemeColor, for: UIControlState.highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: HMNormalFontSize)
        button.setImage(UIImage(named: imgName), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named:"timeline_card_bottom_background"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named:"timeline_card_bottom_background_highlighted"), for: UIControlState.highlighted)
        // 添加控件
        addSubview(button)
        return button
    }
    
    //MARK: - 抽取一个创建竖线的公共方法
    fileprivate func addChildLine() -> UIImageView{
        let img = UIImageView(named: "timeline_card_bottom_line")
        addSubview(img)
        return img
    }
}
